//
//  BookCell.swift
//  LiteAccount
//
//  Created by 刘伟 on 2017/5/11.
//  Copyright © 2017年 刘伟. All rights reserved.
//

import UIKit

class BookCell: FoldingCell {
    
    var data: AccountModel? {
        didSet {
            if let currentData = data {
                amountLabel.text = String(format: "%.2f", currentData.amount)
                noteLabel.text = currentData.note
                detailDateLabel.text = currentData.date
            }
        }
    }

    override func awakeFromNib() {
        foregroundView.layer.cornerRadius = 10
        foregroundView.layer.masksToBounds = true
        super.awakeFromNib()
        initCloseView()
        initOpenView()
    }
    
    // 框架基于 FoldingCell 做 xib ，子视图依旧是 纯代码
    
    func initCloseView() {
        foregroundView.addSubview(leftView)
        leftView.addSubview(yearLabel)
        leftView.addSubview(monthDayLabel)
        leftView.snp.makeConstraints { (make) in
            make.top.left.bottom.equalToSuperview()
            make.width.equalTo(100)
        }
        yearLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.centerY.equalToSuperview().offset(-10)
        }
        monthDayLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.top.equalTo(yearLabel.snp.bottom).offset(10)
        }
        foregroundView.addSubview(rightView)
        rightView.addSubview(lineView)
        rightView.addSubview(amountTitltLabel)
        rightView.addSubview(amountLabel)
        rightView.addSubview(noteTitleLabel)
        rightView.addSubview(noteLabel)
        
        rightView.snp.makeConstraints { (make) in
            make.top.right.bottom.equalToSuperview()
            make.left.equalTo(leftView.snp.right)
        }
        amountTitltLabel.snp.makeConstraints { (make) in
            make.top.equalTo(10)
            make.left.equalTo(10)
        }
        amountLabel.snp.makeConstraints { (make) in
            make.centerX.equalToSuperview()
            make.bottom.equalTo(lineView.snp.top).offset(-5)
        }
        lineView.snp.makeConstraints { (make) in
            make.center.equalToSuperview()
            make.left.equalTo(10)
            make.height.equalTo(0.5)
        }
        
        noteTitleLabel.snp.makeConstraints { (make) in
            make.top.equalTo(lineView.snp.bottom).offset(10)
            make.left.equalTo(10)
        }
        
        noteLabel.snp.makeConstraints { (make) in
            make.top.equalTo(noteTitleLabel.snp.bottom).offset(5)
            make.left.equalTo(10)
            make.right.equalTo(-10)
        }
        
    }
    
    fileprivate func initOpenView() {
        self.containerView.addSubview(detailHeaderView)
        detailHeaderView.addSubview(detailDateLabel)
        detailHeaderView.addSubview(detailBgImageView)
        self.containerView.addSubview(showDetailBtn)
        detailHeaderView.snp.makeConstraints { (make) in
            make.top.left.right.equalToSuperview()
            make.height.equalTo(150)
        }
        detailDateLabel.snp.makeConstraints { (make) in
            make.centerY.equalTo(25)
            make.centerX.equalToSuperview()
        }
        detailBgImageView.snp.makeConstraints { (make) in
            make.top.equalTo(50)
            make.left.bottom.right.equalToSuperview()
        }
        self.containerView.addSubview(detailContentView)
        detailContentView.snp.makeConstraints { (make) in
            make.top.equalTo(150)
            make.left.right.equalToSuperview()
            make.height.equalTo(180)
        }
        showDetailBtn.snp.makeConstraints { (make) in
            make.height.equalTo(50)
            make.left.equalTo(10)
            make.bottom.right.equalTo(-10)
        }
    }
    
    func showDetail() {
    }
    
    
    fileprivate lazy var leftView: UIView = {
        let object = UIView()
        object.backgroundColor = UIColor.la_app
        return object
    }()
    
    fileprivate lazy var yearLabel: UILabel = {
        let object = UILabel()
        object.textColor = .white
        object.font = UIFont.la_Font(ofSize: 15)
        object.textAlignment = .center
        object.text = "2017年"
        return object
    }()
    
    fileprivate lazy var monthDayLabel: UILabel = {
        let object = UILabel()
        object.textColor = .white
        object.font = UIFont.la_Font(ofSize: 15)
        object.textAlignment = .center
        object.text = "05月12日"
        return object
    }()
    
    fileprivate lazy var rightView: UIView = {
        let object = UIView()
        object.backgroundColor = .white
        return object
    }()
    
    
    fileprivate lazy var amountTitltLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 9)
        object.textAlignment = .center
        object.text = "总资产："
        return object
    }()
    
    fileprivate lazy var amountLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 18)
        object.textAlignment = .center
        object.text = "2999999.00"
        return object
    }()
    
    fileprivate lazy var lineView: UIView = {
        let object = UIView()
        object.backgroundColor = UIColor.la_gray
        return object
    }()
    
    fileprivate lazy var noteTitleLabel: UILabel = {
        let object = UILabel()
        object.textColor = .black
        object.font = UIFont.la_Font(ofSize: 9)
        object.textAlignment = .center
        object.text = "备注"
        return object
    }()
    
    fileprivate lazy var noteLabel: UILabel = {
        let object = UILabel()
        object.textColor = UIColor.la_gray
        object.font = UIFont.la_Font(ofSize: 12)
        object.textAlignment = .center
        object.text = "今天记录一下，看一下账本，心情不错"
        return object
    }()
    
    // MARK: 绘制详情页
    fileprivate lazy var detailHeaderView: UIView = {
        let object = UIView()
        object.backgroundColor = UIColor.la_app
        return object
    }()
    
    fileprivate lazy var detailContentView: UIView = {
        let object = UIView()
        object.backgroundColor = UIColor.white
        return object
    }()
    
    fileprivate lazy var detailDateLabel: UILabel = {
        let object = UILabel()
        object.textColor = UIColor.white
        object.font = UIFont.la_Font(ofSize: 15)
        object.textAlignment = .center
        object.text = "2017-04-01"
        return object
    }()
    
    fileprivate lazy var detailAmountLabel: UILabel = {
        let object = UILabel()
        object.textColor = UIColor.la_gray
        object.font = UIFont.la_Font(ofSize: 12)
        object.textAlignment = .center
        object.text = "299999.00"
        return object
    }()
    
    fileprivate lazy var detailNoteLabel: UILabel = {
        let object = UILabel()
        object.textColor = UIColor.la_gray
        object.font = UIFont.la_Font(ofSize: 12)
        object.textAlignment = .center
        object.text = "今天记录一下，看一下账本，心情不错"
        return object
    }()
    
    fileprivate lazy var detailBgImageView: UIImageView = {
        let object = UIImageView()
        object.contentMode = .scaleAspectFill
        object.image = UIImage(named: "bg.jpeg")
        object.clipsToBounds = true
        return object
    }()
    
    fileprivate lazy var showDetailBtn: UIButton = {
        let object = UIButton()
        object.layer.cornerRadius = 2
        object.backgroundColor = UIColor.primary
        object.setTitle("查看详情", for: .normal)
        object.setTitleColor(UIColor.white, for: .normal)
        object.addTarget(self, action: #selector(showDetail), for:
            .touchUpInside)
        return object
    }()
    
    
    

    override func setSelected(_ selected: Bool, animated: Bool) {
        super.setSelected(selected, animated: animated)

        // Configure the view for the selected state
    }
    
    override func animationDuration(_ itemIndex:NSInteger, type:AnimationType)-> TimeInterval {
        
        let durations = [0.26, 0.2, 0.2]
        return durations[itemIndex]
    }
    
}






